Conditions | 1 |
Paths | 8 |
Total Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | |||
53 | function sendRequest(form) |
||
54 | { |
||
55 | $('#toggle-lang-response').trigger('click'); |
||
56 | |||
57 | var $form = $(form); |
||
58 | var $section = $form.closest('section'); |
||
59 | |||
60 | var $btn = $form.find('button[type="submit"]'); |
||
61 | $btn.html($('#preloader-template').html()).attr('disabled', true); |
||
62 | |||
63 | var headers = {}; |
||
64 | $section.find('.headers-form .form-group').not('.except').each(function(key, element) { |
||
65 | var $el = $(element); |
||
66 | if ($el.find('.req-header-active').is(':checked')) { |
||
67 | var header = $el.find('.req-header').val(); |
||
68 | headers[header] = $el.find('.req-header-value').val(); |
||
69 | } |
||
70 | }); |
||
71 | |||
72 | $.ajax({ |
||
73 | url : $section.find('.action-url').val(), |
||
74 | headers: headers, |
||
75 | type : $form.attr('method'), |
||
76 | data : $form.serializeArray(), |
||
77 | success : function(response, status, xhr) { |
||
78 | $btn.text('Send').attr('disabled', false); |
||
79 | $section.find('.method-example-endpoint code.response-content.response-highlighted').jsonViewer(response); |
||
80 | $section.find('.method-example-endpoint code.response-content.response-raw').html(typeof response == 'object' ? JSON.stringify(response): String(response)); |
||
81 | $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders()); |
||
82 | }, |
||
83 | error : function(xhr) { |
||
84 | $btn.text('Send').attr('disabled', false); |
||
85 | var content = xhr.responseText; |
||
86 | if (xhr.statusText && !content) { |
||
87 | $.notify({ |
||
88 | message: xhr.statusText |
||
89 | },{ |
||
90 | type: 'danger' |
||
91 | }); |
||
92 | } |
||
93 | if (IsJsonString(content)) { |
||
94 | $section.find('.method-example-endpoint code.response-content').jsonViewer(content); |
||
95 | return; |
||
96 | } |
||
97 | var $frame = $('<iframe class="supa" style="width:100%; height:350px;">'); |
||
98 | $section.find('.method-example-endpoint code.response-content.response-highlighted').html($frame); |
||
99 | $section.find('.method-example-endpoint code.response-content.response-raw').html(typeof content == 'object' ? JSON.stringify(content): String(content)); |
||
100 | setTimeout(function() { |
||
101 | var doc = $frame[0].contentWindow.document; |
||
102 | var $body = $('body', doc); |
||
103 | $body.html(content); |
||
104 | }, 1); |
||
105 | |||
106 | $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders()); |
||
107 | } |
||
108 | }); |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | |||
162 |